from math import sin, cos, sqrt, atan2

dotp    = lambda u, v: sum([u[i]*v[i] for i in range(3)])
scalarp = lambda s, v: tuple([s*v[i] for i in range(3)])
vsum    = lambda u, v: tuple([u[i] + v[i] for i in range(3)])
vdiff   = lambda u, v: tuple([u[i] - v[i] for i in range(3)])
mag     = lambda    v: sqrt(sum([v[i]*v[i] for i in range(3)]))
magsq   = lambda    v: sum([v[i]*v[i] for i in range(3)])
crossp  = lambda u, v: (u[1]*v[2] - u[2]*v[1], -(u[0]*v[2] - u[2]*v[0]), u[0]*v[1] - u[1]*v[0])

def norm(v):
    m = 1.0/mag(v)
    return tuple([v[i]*m for i in range(3)])

def rotx(v, th):
    ct = cos(th)
    st = sin(th)
    return (v[0],
            ct*v[1] - st*v[2],
            st*v[1] + ct*v[2])

def roty(v, th):
    ct = cos(th)
    st = sin(th)
    return (ct*v[0] + st*v[2],
            v[1],
            -st*v[0] + ct*v[2])
    
def rotz(v, th):
    ct = cos(th)
    st = sin(th)
    return (ct*v[0] - st*v[1],
            st*v[0] + ct*v[1],
            v[2])

def findTPAngles(v):
    x, y, z = v
    m = sqrt(x*x + z*z)
    t = -atan2(y, m)
    p = atan2(x, z)
    return t, p

    